﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace INTROSE
{
    public partial class Add_Transaction : Form
    {
        SqlConnection con = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = Introse; Integrated Security = True");
        private string name, position;
        private DateTime date = new DateTime();
        public Add_Transaction(string name, string position)
        {
            InitializeComponent();
            this.name = name;
            this.position = position;
            date = System.DateTime.Now;
            txtDateOrdered.Text = date.Date.ToString();
            con.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM sizes WHERE size = '100cc'", con);
            SqlDataReader reader = command.ExecuteReader();
            if(reader.HasRows)
            {
                reader.Read();
                txtp100cc.Text = reader.GetInt32(1).ToString();
            }
            reader.Close();
            command = new SqlCommand("SELECT * FROM sizes WHERE size = '200cc'", con);
            reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                txtp200cc.Text = reader.GetInt32(1).ToString();
            }
            reader.Close();
            command = new SqlCommand("SELECT * FROM sizes WHERE size = '250cc'", con);
            reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                txtp250cc.Text = reader.GetInt32(1).ToString();
            }
            reader.Close();
            command = new SqlCommand("SELECT * FROM sizes WHERE size = '400cc'", con);
            reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                txtp400cc.Text = reader.GetInt32(1).ToString();
            }
            reader.Close();
            command = new SqlCommand("SELECT * FROM sizes WHERE size = '500cc'", con);
            reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                txtp500cc.Text = reader.GetInt32(1).ToString();
            }
            reader.Close();
            con.Close();
            txt100cc.Text = "0";
            txt200cc.Text = "0";
            txt250cc.Text = "0";
            txt400cc.Text = "0";
            txt500cc.Text = "0";
        }

        private void Add_Transaction_Load(object sender, EventArgs e)
        {
            txt100cc.Enabled = false;
            txt200cc.Enabled = false;
            txt250cc.Enabled = false;
            txt400cc.Enabled = false;
            txt500cc.Enabled = false;
            txtp100cc.Enabled = false;
            txtp200cc.Enabled = false;
            txtp250cc.Enabled = false;
            txtp400cc.Enabled = false;
            txtp500cc.Enabled = false;
            btnSave.Enabled = false;
        }

        private void chckBox100cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox100cc.Checked)
            {
                txt100cc.Enabled = true;
                txtp100cc.Enabled = true;
            }
            else
            {
                txt100cc.Enabled = false;
                txtp100cc.Enabled = false;
                txt100cc.Text = "0";
            }
        }

        private void chckBox200cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox200cc.Checked)
            {
                txt200cc.Enabled = true;
                txtp200cc.Enabled = true;
            }
            else
            {
                txt200cc.Enabled = false;
                txtp200cc.Enabled = false;
                txt200cc.Text = "0";
            }
        }

        private void chckBox250cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox250cc.Checked)
            {
                txt250cc.Enabled = true;
                txtp250cc.Enabled = true;
            }
            else
            {
                txt250cc.Enabled = false;
                txtp250cc.Enabled = false;
                txt250cc.Text = "0";
            }
        }

        private void chckBox400cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox400cc.Checked)
            {
                txt400cc.Enabled = true;
                txtp400cc.Enabled = true;
            }
            else
            {
                txt400cc.Enabled = false;
                txtp400cc.Enabled = false;
                txt400cc.Text = "0";
            }
        }

        private void chckBox500cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox500cc.Checked)
            {
                txt500cc.Enabled = true;
                txtp500cc.Enabled = true;
            }
            else
            {
                txt500cc.Enabled = false;
                txtp500cc.Enabled = false;
                txt500cc.Text = "0";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int checker = 0;
            if (radioButtonClaimed.Checked == false && radioButtonUnclaimed.Checked == false)
            {
                MessageBox.Show("Please choose if claimed or unclaimed.");
                checker = 0;
            }
            if (radioButtonPaid.Checked == false && radioButtonUnpaid.Checked == false)
            {
                MessageBox.Show("Please choose if paid or unpaid.");
                checker = 0;
            }
            if (radioButtonDelivery.Checked == false && radioButtonPickUp.Checked == false)
            {
                MessageBox.Show("Please choose one under mode of requisition.");
                checker = 0;
            }
            if(txtName.Text == "" || txtAddress.Text == "")
            {
                MessageBox.Show("Please fill up the name field and/or the address field.");
                checker = 0;
            }
            if (((radioButtonClaimed.Checked == true && radioButtonUnclaimed.Checked == false) || (radioButtonClaimed.Checked == false && radioButtonUnclaimed.Checked == true)) && ((radioButtonPaid.Checked == true && radioButtonUnpaid.Checked == false) || (radioButtonPaid.Checked == false && radioButtonUnpaid.Checked == true)) && ((radioButtonDelivery.Checked == true && radioButtonPickUp.Checked == false) || (radioButtonDelivery.Checked == false && radioButtonPickUp.Checked == true)) && txtName.Text != "" && txtAddress.Text != "")
                checker = 1;
            if (checker == 1) 
            {
                con.Open();
                SqlCommand command;
                DateTime date1 = new DateTime();
                date1 = System.DateTime.Now;
                if (radioButtonClaimed.Checked == true && radioButtonPaid.Checked == true)
                {
                    command = new SqlCommand("Insert into orders values('" + date1 + "', '" + txtName.Text + "', '" + txtAddress.Text + "', '" + date1 + "', '0', '0', '" + date1 + "', '0', '0')", con);
                    command.ExecuteNonQuery();
                }
                else if (radioButtonClaimed.Checked == true && radioButtonPaid.Checked == false)
                {
                    command = new SqlCommand("Insert into orders values('" + date1 + "', '" + txtName.Text + "', '" + txtAddress.Text + "', ' ', '0', '0', '" + date1 + "', '0', '0')", con);
                    command.ExecuteNonQuery();
                }
                else if (radioButtonClaimed.Checked == false && radioButtonPaid.Checked == true)
                {
                    command = new SqlCommand("Insert into orders values('" + date1 + "', '" + txtName.Text + "', '" + txtAddress.Text + "', '" + date1 + "', '0', '0', ' ', '0', '0')", con);
                    command.ExecuteNonQuery();
                }
                else if (radioButtonClaimed.Checked == false && radioButtonPaid.Checked == false)
                {
                    command = new SqlCommand("Insert into orders values('" + date1 + "', '" + txtName.Text + "', '" + txtAddress.Text + "', '', '0', '0', ' ', '0', '0')", con);
                    command.ExecuteNonQuery();
                }
                if (radioButtonDelivery.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET modDistr = '1'", con);
                    command.ExecuteNonQuery();
                    if (radioButtonPaid.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET paid = '1', datePay = '" + date + "'", con);
                        command.ExecuteNonQuery();
                    }
                    else if (radioButtonUnpaid.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET paid = '0'", con);
                        command.ExecuteNonQuery();
                    }
                    if (radioButtonClaimed.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET delivered = '1', claimed = '" + null + "'", con);
                        command.ExecuteNonQuery();
                    }
                    else if (radioButtonUnclaimed.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET delivered = '0', claimed = '" + null + "'", con);
                        command.ExecuteNonQuery();
                    }
                }
                else if (radioButtonPickUp.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET modDistr = '0'", con);
                    command.ExecuteNonQuery();
                    if (radioButtonPaid.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET paid = '1'", con);
                        command.ExecuteNonQuery();
                    }
                    else if (radioButtonUnpaid.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET paid = '0'", con);
                        command.ExecuteNonQuery();
                    }
                    if (radioButtonClaimed.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET delivered = '" + null + "', claimed = '1', dateClaim = '" + date + "'", con);
                        command.ExecuteNonQuery();
                    }
                    else if (radioButtonUnclaimed.Checked)
                    {
                        command = new SqlCommand("UPDATE orders SET delivered = '" + null + "', claimed = '0'", con);
                        command.ExecuteNonQuery();
                    }
                }
                SqlCommand command1 = new SqlCommand("SELECT transNo FROM orders where custName = '" + txtName.Text + "' AND transDate = '" + date1 + "'", con);
                SqlDataReader reader = command1.ExecuteReader();
                int a = 0;
                if (reader.HasRows)
                {
                    reader.Read();
                    a = reader.GetInt32(0);
                }
                reader.Close();
                SqlCommand com = new SqlCommand("INSERT INTO orderItems VALUES('" + a + "', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')", con);
                com.ExecuteNonQuery();
                if (chckBox100cc.Checked)
                {
                    int z=int.Parse(txt100cc.Text), x=int.Parse(txtp100cc.Text);
                    com = new SqlCommand("UPDATE orderItems SET q100cc = '" + z + "', p100cc = '" + x + "' WHERE transNo = '" + a + "'", con);
                    com.ExecuteNonQuery();
                }
                if (chckBox200cc.Checked)
                {
                    int z = int.Parse(txt200cc.Text), x = int.Parse(txtp200cc.Text);
                    com = new SqlCommand("UPDATE orderItems SET q200cc = '" + z + "', p200cc = '" + x + "' WHERE transNo = '" + a + "'", con);
                    com.ExecuteNonQuery();
                }
                if (chckBox250cc.Checked)
                {
                    int z = int.Parse(txt250cc.Text), x = int.Parse(txtp250cc.Text);
                    com = new SqlCommand("UPDATE orderItems SET q250cc = '" + z + "', p250cc = '" + x + "' WHERE transNo = '" + a + "'", con);
                    com.ExecuteNonQuery();
                }
                if (chckBox400cc.Checked)
                {
                    int z = int.Parse(txt400cc.Text), x = int.Parse(txtp400cc.Text);
                    com = new SqlCommand("UPDATE orderItems SET q400cc = '" + z + "', p400cc = '" + x + "' WHERE transNo = '" + a + "'", con);
                    com.ExecuteNonQuery();
                }
                if (chckBox500cc.Checked)
                {
                    int z = int.Parse(txt500cc.Text), x = int.Parse(txtp500cc.Text);
                    com = new SqlCommand("UPDATE orderItems SET q500cc = '" + z + "', p500cc = '" + x + "' WHERE transNo = '" + a + "'", con);
                    com.ExecuteNonQuery();
                }
                MessageBox.Show("Successfully added a transaction! Transaction Number is : " + a);
                int b = int.Parse(txtp100cc.Text);
                SqlCommand price = new SqlCommand("UPDATE sizes SET price='" + b + "' WHERE size = '100cc'", con);
                price.ExecuteNonQuery();
                b = int.Parse(txtp200cc.Text);
                price = new SqlCommand("UPDATE sizes SET price='" + b + "' WHERE size = '200cc'", con);
                price.ExecuteNonQuery();
                b = int.Parse(txtp250cc.Text);
                price = new SqlCommand("UPDATE sizes SET price='" + b + "' WHERE size = '250cc'", con);
                price.ExecuteNonQuery();
                b = int.Parse(txtp400cc.Text);
                price = new SqlCommand("UPDATE sizes SET price='" + b + "' WHERE size = '400cc'", con);
                price.ExecuteNonQuery();
                b = int.Parse(txtp500cc.Text);
                price = new SqlCommand("UPDATE sizes SET price='" + b + "' WHERE size = '500cc'", con);
                price.ExecuteNonQuery();
                price = new SqlCommand("UPDATE orderItems SET total = '" + txtTotal.Text + "' WHERE transNo = '" + a + "'", con);
                this.Close();
                MenuExecutive menu = new MenuExecutive(name, position);
                menu.Show();
                con.Close();
            }
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
            MenuExecutive menu = new MenuExecutive(name, position);
            menu.Show();
        }
        
        private void radioButtonPaid_CheckedChanged(object sender, EventArgs e)
        {
            this.date = System.DateTime.Now;
            if (radioButtonPaid.Checked == true)
                txtDatePaid.Text = this.date.Date.ToString();
            else
                txtDatePaid.Text = "";
        }

        private void radioButtonClaimed_CheckedChanged(object sender, EventArgs e)
        {
            this.date = System.DateTime.Now;
            if (radioButtonClaimed.Checked == true)
                txtDateClaimed.Text = this.date.Date.ToString();
            else
                txtDateClaimed.Text = "";
        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
            int txt100 = 0, txt200 = 0, txt250 = 0, txt400 = 0, txt500 = 0, total = 0, checker1 = 0, checker2 = 0, checker3 = 0, checker4 = 0, checker5 = 0;
            if (txt100cc.Text == "" && txt200cc.Text == "" && txt250cc.Text == "" && txt400cc.Text == "" && txt500cc.Text == "")
                MessageBox.Show("No orders were made! Please order atleast one");
            if (chckBox100cc.Checked == true || chckBox200cc.Checked == true || chckBox250cc.Checked == true || chckBox400cc.Checked == true || chckBox500cc.Checked == true)
            {
                if (txt100cc.Text == "")
                    txt100cc.Text = "0";
                else if (chckBox100cc.Checked && (txt100cc.Text[0] < 48 || txt100cc.Text[0] >= 57 || Int32.Parse(txt100cc.Text) == 0))
                {
                    checker1 = 1;
                    if (chckBox100cc.Checked && (txt100cc.Text[0] < 48 || txt100cc.Text[0] >= 57))
                        MessageBox.Show("Please enter numbers only");
                    else if (chckBox100cc.Checked && Int32.Parse(txt100cc.Text) == 0)
                        MessageBox.Show("Please enter a number not equal to 0");
                }
                else
                {
                    checker1 = 0;
                    txt100 = Int32.Parse(txt100cc.Text);
                }
                if (txt200cc.Text == "")
                    txt200cc.Text = "0";
                else if (chckBox200cc.Checked && (txt200cc.Text[0] < 48 || txt200cc.Text[0] >= 57 || Int32.Parse(txt200cc.Text) == 0))
                {
                    checker2 = 1;
                    if (chckBox200cc.Checked && (txt200cc.Text[0] < 48 || txt200cc.Text[0] >= 57))
                        MessageBox.Show("Please enter numbers only");
                    else if (chckBox200cc.Checked && Int32.Parse(txt200cc.Text) == 0)
                        MessageBox.Show("Please enter a number not equal to 0");
                }
                else
                {
                    checker2 = 0;
                    txt200 = Int32.Parse(txt200cc.Text);
                }
                if (txt250cc.Text == "")
                    txt250cc.Text = "0";
                else if (chckBox250cc.Checked && (txt250cc.Text[0] < 48 || txt250cc.Text[0] >= 57 || Int32.Parse(txt250cc.Text) == 0))
                {
                    checker3 = 1;
                    if (chckBox250cc.Checked && (txt250cc.Text[0] < 48 || txt250cc.Text[0] >= 57))
                        MessageBox.Show("Please enter numbers only");
                    else if (chckBox250cc.Checked && Int32.Parse(txt250cc.Text) == 0)
                        MessageBox.Show("Please enter a number not equal to 0");
                }
                else
                {
                    checker3 = 0;
                    txt250 = Int32.Parse(txt250cc.Text);
                }
                if (txt400cc.Text == "")
                    txt400cc.Text = "0";
                else if (chckBox400cc.Checked && (txt400cc.Text[0] < 48 || txt400cc.Text[0] >= 57 || Int32.Parse(txt400cc.Text) == 0))
                {
                    checker4 = 1;
                    if (chckBox400cc.Checked && (txt400cc.Text[0] < 48 || txt400cc.Text[0] >= 57))
                        MessageBox.Show("Please enter numbers only");
                    else if (chckBox400cc.Checked && Int32.Parse(txt400cc.Text) == 0)
                        MessageBox.Show("Please enter a number not equal to 0");
                }
                else
                {
                    checker4 = 0;
                    txt400 = Int32.Parse(txt400cc.Text);
                }
                if (txt500cc.Text == "")
                    txt500cc.Text = "0";
                else if (chckBox500cc.Checked && (txt500cc.Text[0] < 48 || txt500cc.Text[0] >= 57 || Int32.Parse(txt500cc.Text) == 0))
                {
                    checker5 = 1;
                    if (chckBox500cc.Checked && (txt500cc.Text[0] < 48 || txt500cc.Text[0] >= 57))
                        MessageBox.Show("Please enter numbers only");
                    else if (chckBox500cc.Checked && Int32.Parse(txt500cc.Text) == 0)
                        MessageBox.Show("Please enter a number not equal to 0");
                }
                else
                {
                    checker5 = 0;
                    txt500 = Int32.Parse(txt500cc.Text);
                }
            }
            else
                MessageBox.Show("Please check atleast one size of the product.");
            if (checker1 == 0 && checker2 == 0 && checker3 == 0 && checker4 == 0 && checker5 == 0)
            {
                total = (txt100 * Int32.Parse(txt100cc.Text)) + (txt200 * Int32.Parse(txt200cc.Text)) + (txt250 * Int32.Parse(txt250cc.Text)) + (txt400 * Int32.Parse(txt400cc.Text)) + (txt500 * Int32.Parse(txt500cc.Text));
                txtTotal.Text = total.ToString();
                btnSave.Enabled = true;
            }
            else
                txtTotal.Text = "0";
        }
    }
}
